package sse.ustc.cc.fragment;

import java.util.ArrayList;

import sse.ustc.cc.activity.CourseDetailAty;
import sse.ustc.cc.activity.R;
import sse.ustc.cc.activity.CourseAty;
import sse.ustc.cc.custom.CustomWeekView;
import sse.ustc.cc.custom.CustomWeekView.OnItemClassClickListener;

import sse.ustc.cc.db.DBService;
import sse.ustc.cc.util.ClassInfo;
import sse.ustc.cc.util.MyLinearLayout;
import sse.ustc.cc.util.ScaleGestureListener;
import sse.ustc.cc.util.SettingInfo;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.ScaleGestureDetector.OnScaleGestureListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class WeekClassInfoFrag extends Fragment {
	//test
	protected static final Fragment AddCourseFragment = null;
	private int weekNum;
	private LayoutInflater inflater;
	private Context mContext;
	boolean longpush = false;
	private final static String TAG = "week_class";
	private CustomWeekView weekView;
	private ArrayList<ClassInfo> classList;// 获取课表信息
	private ArrayList<SettingInfo> settingList, dyweekList;// 获取设置和当前周数信息
	private ArrayList<String> courseList;// 获取课程信息
	private ArrayList<String> setList;// 获取设置信息

	public static WeekClassInfoFrag newInstance(int num) {
		WeekClassInfoFrag f = new WeekClassInfoFrag();
		Bundle args = new Bundle();
		args.putInt("num", num);
		f.setArguments(args);

		return f;
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		mContext = activity;

		super.onAttach(activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		weekNum = getArguments() != null ? getArguments().getInt("num") : 1;
		/* 初始化 */
//		courseList = new ArrayList<String>();
//		setList = new ArrayList<String>();
//		dyweekList = new ArrayList<SettingInfo>();
//		dyweekList.clear();// 清空
//		DBService ds = new DBService(mContext);
//		courseList = ds.selectCourse();// 选择课表信息，存放在list里面
//		setList = ds.selectSetting();// 选择设置信息，存放在list里面
		inflater = LayoutInflater.from(mContext);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreateView(inflater, container, savedInstanceState);
		MyLinearLayout myLinearLayout = new MyLinearLayout(mContext, null);

		LinearLayout LinearLayout = (android.widget.LinearLayout) inflater
				.inflate(R.layout.view_week_classinfo, container, false);

		myLinearLayout.addView(LinearLayout);
		weekView = (CustomWeekView) LinearLayout.findViewById(R.id.scheduleView);
		/* 初始化 */
		courseList = new ArrayList<String>();
		setList = new ArrayList<String>();
		dyweekList = new ArrayList<SettingInfo>();
		dyweekList.clear();// 清空
		DBService ds = new DBService(mContext);
		courseList = ds.selectCourse();// 选择课表信息，存放在list里面
		setList = ds.selectSetting();// 选择设置信息，存放在list里面
		/*判断课程表是否存在课程*/
		if (courseList != null && courseList.size() > 0) {//存在课程信息
			//int weekend = Integer.parseInt(courseList.get(10));// 获取Week_end
			int week = Integer.parseInt(setList.get(0)) + weekNum;

			SettingInfo settingInfo = new SettingInfo();
			settingInfo.setDyweek(week + "");
			dyweekList.add(settingInfo);
			
			String weektime = week + "";
			getClassData(weektime);//根据周数选择课程信息
			getSettingData();//获取设置列表的信息
			weekView.setClassList(classList);// 将课程信息导入到View中
			//weekView.setSettingList(settingList);// 将设置信息导入到View中
			weekView.setDyweekList(dyweekList);// 将动态周数信息导入到View中
			/*长按效果的监听*/
			weekView.setOnLongClickListener(new OnLongClickListener() {
				@Override
				public boolean onLongClick(View v) {

					Toast.makeText(mContext, "I got a long press!",
							Toast.LENGTH_SHORT).show();
					longpush = true;
					return false;
				}
			});
			/*点击课程效果的监听*/
			weekView.setOnItemClassClickListener(new OnItemClassClickListener() {

				@Override
				public void onClick(ClassInfo classInfo) {
					if (!longpush) {
						Toast.makeText(mContext,
								"您点击的课程是：" + classInfo.getClassname(),
								Toast.LENGTH_SHORT).show();
						// 实例化 Bundle，设置需要传递的参数 
						Intent intent = new Intent(getActivity(),CourseDetailAty.class);
				        ArrayList<ClassInfo> courseInfoList = getCourseInfo(classInfo.getClassname());
				        intent.putExtra("courseinfo",courseInfoList);
						startActivity(intent);
						longpush = false;
					}
					longpush = false;
				}
				/**
				 * 获得与点击课程名称相同的课程信息
				 * @param courseName
				 * @return
				 */
				private ArrayList<ClassInfo> getCourseInfo(String courseName) {
					ArrayList<ClassInfo> courseInfoList = new ArrayList<ClassInfo>();
					for(int i = 0;i<classList.size();i++){
						if(courseName.equals(classList.get(i).getClassname())){
							courseInfoList.add(classList.get(i));
						}
					}
					return courseInfoList;
				}
			});
			/*点击其他区域效果的监听*/
//			weekView.setonItemNoneClassClickListener(new onItemNoneClassClickListener() {
//
//				@Override
//				public void onClick() {
//					// TODO Auto-generated method stub
//
//					//addCourseDdialog();
//
//				}
//			});
		}else{//不存在课程信息，提示添加课程
			//addCourseDdialog();
		}

		
		// 添加手势缩放
		ScaleGestureListener sg = new ScaleGestureListener(myLinearLayout,
				mContext, TAG);
		final ScaleGestureDetector mScaleGestureDetector = new ScaleGestureDetector(
				getActivity(), sg);
		myLinearLayout.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				return mScaleGestureDetector.onTouchEvent(event);
			}
		});

		return myLinearLayout;
	}
	/**
	* 提示增加课程
	*/
	protected void addCourseDdialog() {
		AlertDialog.Builder builder = new Builder(getActivity());
		builder.setMessage("添加课程否？");
		builder.setTitle("提示");
		builder.setPositiveButton("是", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				 /*跳转到课程增加界面*/
				Intent intent = new Intent();
				intent.setClass(mContext, CourseAty.class);
				Bundle bundle = new Bundle();
				bundle.putString("addcourse", "add");
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});
		builder.setNegativeButton("否", new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}

	/**
	* 提获取设置信息表内容
	*/
	private void getSettingData() {
		settingList = new ArrayList<SettingInfo>();
		SettingInfo settingInfo = new SettingInfo();
		settingInfo.setCurrentWeek(setList.get(0));
		settingInfo.setMaxSection(setList.get(1));
		settingInfo.setSectionTime(setList.get(2));
		settingList.add(settingInfo);

	}

	/**
	* 根据周数来提获取课程信息表内容
	* @param 参数为第几周
	*/
	private void getClassData(String weektime) {
		classList = new ArrayList<ClassInfo>();
		int j = courseList.size() / 13;// 确定有几门课程
		Log.e("课程信息0： ", courseList.get(0));
		Log.e("课程信息1： ", courseList.get(1));
		Log.e("课程信息2： ", courseList.get(2));
		Log.e("课程信息3： ", courseList.get(3));
		Log.e("课程信息4： ", courseList.get(4));
		Log.e("课程信息5： ", courseList.get(5));
		Log.e("课程信息6： ", courseList.get(6));
		Log.e("课程信息7： ", courseList.get(7));
		Log.e("课程信息8： ", courseList.get(8));
		Log.e("课程信息9： ", courseList.get(9));
		Log.e("课程信息10： ", courseList.get(10));
		Log.e("课程信息11： ", courseList.get(11));
		Log.e("课程信息12： ", courseList.get(12));

		for (int i = 0; i < j; i++) {
			/* 显示当前周数有课的课程信息 */
			if (Integer.parseInt(courseList.get(13 * i + 10)) >= Integer
					.parseInt(weektime)
					&& Integer.parseInt(weektime) >= Integer
							.parseInt(courseList.get(13 * i + 3))) {
				ClassInfo calssInfo = new ClassInfo();
				calssInfo.setClassname(courseList.get(13 * i));//获取课程名称
				calssInfo.setClassRoom(courseList.get(13 * i + 8));//获取课程的教室
				calssInfo.setTeacher_name(courseList.get(13 * i + 4));
				
				calssInfo.setWeek_begin(courseList.get(13 * i + 3));//获取周数结束时间
				calssInfo.setWeek_end(courseList.get(13 * i + 10));//获取周数结束时间
				
				calssInfo.setFromClassNum(Integer.parseInt(courseList
						.get(13 * i + 2)));//获取课程 节数的开始
				calssInfo.setWeekday(Integer.parseInt(courseList.get(13 * i + 1)));//获取周几
				calssInfo.setSection_end(courseList.get(13 * i + 9));//获取课程 节数的结束
				calssInfo.setSection_begin(courseList.get(13 * i + 2));//获取课程 节数的结束
				
				calssInfo.setClassNumLen(Integer.parseInt(courseList.get(13 * i + 9))
						- Integer.parseInt(courseList.get(13 * i + 2)) + 1);//课程节数的间隔
				calssInfo.setCourse_begin_time(courseList.get(13 * i + 11));//获取周数结束时间
				calssInfo.setCourse_end_time(courseList.get(13 * i + 12));//获取周数结束时间
				Log.i(TAG, ">>>>>>>>>>>>>>>getWeekday"+calssInfo.getWeek_begin());
				classList.add(calssInfo);
			}

		}
	}
}
